package com.sjzl.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ModelDriven;
import com.sjzl.dto.UserDto;
import com.sjzl.entity.Friend;
import com.sjzl.entity.Notification;
import com.sjzl.entity.Post;
import com.sjzl.entity.User;
import com.sjzl.service.FriendService;
import com.sjzl.service.NotificationService;
import com.sjzl.service.UserService;
import com.sjzl.util.ShowMessageWindow;
import com.sjzl.util.Warning;

@SuppressWarnings("rawtypes")
@Component("userAction")
@Scope("prototype")
public class UserAction extends BaseAction implements ModelDriven {

	@Resource(name="userServiceImpl")
	private UserService userService;
	
	@Resource(name="friendServiceImpl")
	private FriendService friendService;
	
	@Resource(name="notificationServiceImpl")
	private NotificationService notificationService;
	
	private UserDto userDto = new UserDto();
	
	public Object getModel() {
		return userDto;
	}
	
	private Warning warning;
	public Warning getWarning() {
		return warning;
	}
	
	private ShowMessageWindow smw;
	public ShowMessageWindow getSmw() {
		return smw;
	}

	private User user;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	private List<Friend> friends;
	public List<Friend> getFriends() {
		return friends;
	}
	public void setFriends(List<Friend> friends) {
		this.friends = friends;
	}

	//为了防止和modelDriven中的account重名，所以这里用selectAccount接收用户传回来的用户名
	private String selectAccount;
	public String getSelectAccount() {
		return selectAccount;
	}
	public void setSelectAccount(String selectAccount) {
		this.selectAccount = selectAccount;
	}
	
	private List<Post> posts;
	public List<Post> getPosts() {
		return posts;
	}
	
	private Notification notification;
	public Notification getNotification() {
		return notification;
	}
	public void setNotification(Notification notification) {
		this.notification = notification;
	}
	
	private Integer n_id;
	public Integer getN_id() {
		return n_id;
	}
	public void setN_id(Integer n_id) {
		this.n_id = n_id;
	}

	private List<Notification> notifications;
	public List<Notification> getNotifications() {
		return notifications;
	}
	public void setNotifications(List<Notification> notifications) {
		this.notifications = notifications;
	}
	private File upload;//得到上传的文件
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	
	private String uploadContentType;//得到文件的类型
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	  
	private String uploadFileName;//得到文件的名称
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	/**
	 * 登陆
	 * @return
	 */
	public String login(){
		User sessionUser = (User) httpSession.getAttribute("sessionUser");
		if(sessionUser != null){
			actionContext.getSession().put("errorMsg", "不能重复登录，请注销当前用户后再做此操作！");
			try {
				response.sendRedirect("error.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		if(userService.login(userDto, userDto.getTimeout(), request, response)){
			return "login_success";
		}
		warning = new Warning("用户名或密码不正确！");
		//request.setAttribute("message", "用户名或密码不正确！");
		return "login";
	}
	
	/**
	 * 注销
	 * @return
	 */
	public String logout(){
		userService.logout(request, response);
		return "logout_success";
	}
	
	/**
	 * 注册
	 * @return
	 */
	public String register(){
		user.setDateCreated(new Date());
		user.setIntegral(25);
		user.setPostCount(0);
		user.setLevel(1);
		userService.save(user);
		return "register_success";
	}
	
	/**
	 * 进入我的个人空间
	 * @return
	 */
	public String myselfInfo(){
		user = userService.getUserByAccount(selectAccount);
		friends = user.getFriends();
		this.setTitle(user.getAccount()+"的个人空间主页");
		return "myselfInfo";
	}
	
	/**
	 * 修改资料页面
	 * @return
	 */
	public String updateUserUI(){
		user = userService.getUserByAccount(selectAccount);
		this.setTitle("修改"+user.getAccount()+"的资料");
		return "updateUserUI";
	}
	
	/**
	 * 修改我的资料
	 * @return
	 */
	public String updateUser(){
		User u = userService.getUserByAccount(user.getAccount());
		//不要更新的字段
		BeanUtils.copyProperties(user, u, new String[]{"account", "password","dateCreated","id","email","deleted","version","dateLastActived","ipCreated","permission","level","postVisitors_id","integral","postCount"});
		userService.updateUserInfo(u, upload, uploadFileName, uploadContentType);
		return "updateUser_success";
	}
	
	/**
	 * 查看我的好友
	 * @return
	 */
	public String myfriendUI(){
		user = userService.getUserByAccount(selectAccount);
		friends = user.getFriends();
		return "myfriendUI";
	}
	
	public String selectMessageUI(){
		user = userService.getUserByAccount(selectAccount);
		notifications = user.getNotifications();
		return "selectMessageUI";
	}
	
	/**
	 * 查看留言
	 * @return
	 */
	@Transactional
	public String selectMessage(){
		notification = notificationService.get(Notification.class, n_id);
		smw = new ShowMessageWindow(notification.getContent());
		notification.setIsNew(0);
		notificationService.update(notification);
		return selectMessageUI();
	}
	
	/**
	 * 删除留言
	 * @return
	 */
	public String deleteMessage(){
		notification = notificationService.get(Notification.class, n_id);
		notificationService.delete(notification);
		return selectMessageUI();
	}
	
	/**
	 * 进入发表心情输入界面
	 * @return
	 */
	public String releaseMoodUI(){
		user = userService.getUserByAccount(selectAccount);
		this.setTitle("编辑我的心情");
		return "releaseMoodUI";
	}
	
	/**
	 * 发布心情
	 * @return
	 */
	public String releaseMood(){
		User sessionUser = (User) httpSession.getAttribute("sessionUser");
		User dbUser = userService.get(User.class, sessionUser.getId());
		dbUser.setMood(userDto.getMood());
		userService.update(dbUser);
		this.setMessage(" -- 成功发表心情");
		this.setTitle("成功发表心情");
		return releaseMoodUI();
	}
	
	
	//--------------------------------------------模块分界线--------------------------------------------------
	
	/**
	 * 查询其他用户
	 * @return
	 */
	public String selectUser(){
		user = userService.getUserByAccount(selectAccount);
		this.setTitle("查看"+user.getAccount()+"的基本信息");
		return "selectUser";
	}
	
	/**
	 * 查询其他用户的相关帖子
	 * @return
	 */
	public String selectUserPosts(){
		user = userService.getUserByAccount(selectAccount);
		posts = user.getPosts();
		this.setTitle("查看"+user.getAccount()+"所创建帖子");
		return "selectUserPosts";
	}
	
	/**
	 * 加为好友
	 * @return
	 */
	public String addToFriend(){
		user = userService.getUserByAccount(selectAccount);
		if(friendService.create(user)){
			warning = new Warning("成功将"+user.getAccount()+"加为我的好友");
			return "addToFriend_success";
		}
		warning = new Warning("不能重复添加该好友，或者添加自己为好友");
		return "addToFriend_success";
	}
	
	
	
	/**
	 * 留言编辑界面
	 * @return
	 */
	public String giveToMessageUI(){
		user = userService.getUserByAccount(selectAccount);
		friends = user.getFriends();
		return "giveToMessageUI";
	}
	
	/**
	 * 留言
	 * @return
	 */
	public String giveToMessage(){
		try{
			if(notificationService.create(notification, selectAccount)){
				warning = new Warning("成功给"+selectAccount+"留言，请静心等待回复！");
				return giveToMessageUI();
			}
		}catch(Exception e){
			warning = new Warning("对不起，由于系统原因，你的留言没有正式提交，请重试！");
			return giveToMessageUI();
		}
		warning = new Warning("留言失败！");
		return giveToMessageUI();
	}


	
}
